package com.hgts.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hgts.common.R;
import com.hgts.pojo.Sign;
import com.hgts.service.ISignService;
import com.hgts.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 曹怀金
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/sign")
@Slf4j
public class SignController {

    @Autowired
    private ISignService signService;

    // 新增或者更新
    @GetMapping("/{location}")
    public R<String> save(@PathVariable String location) {
        log.info("船只：{}",location);
        //获取当前登入用户
        Long userId = (long) UserHolder.getUser().getId();
        //根据id查询是否签到
        Sign sign = new Sign();
        // 新增打卡
            String today = DateUtil.today();
            LambdaQueryWrapper<Sign> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Sign::getUserId, userId);
            queryWrapper.eq(Sign::getTime, today);
            Sign one = signService.getOne(queryWrapper);
            if (one != null) {  // 打过卡了
                return R.error( "您已打过卡");
            }
            sign.setTime(today);
            sign.setLocation(location);
            sign.setUserId(userId);

        signService.saveOrUpdate(sign);
        return R.success("打卡成功");
    }

    @GetMapping
    public R<List<Sign>> getSignDate(){
        //查询并返回
        List<Sign> list = signService.list();

        return R.success(list);
    }
}

